//
//  HeadInfoCell.m
//  mendianjieke
//
//  Created by 响当当 on 8/2/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HeadInfoCell.h"

@interface HeadInfoCell()
@property (nonatomic, strong)UIImageView *bgImageView;//背景
@property (nonatomic, strong)UIImageView *headImageView;//头像
@property (nonatomic, strong)UILabel *headLabel;//
@property (nonatomic, strong)UIButton *stateButton;//营业状态
@property (nonatomic, strong)UILabel *shopLabel;//店名
@property (nonatomic, strong)UISwitch *deviceState;//设备状态
@end

@implementation HeadInfoCell

- (UILabel *)headLabel
{
    if (!_headLabel)
    {
        _headLabel = [Utilties creatLabelWithFrame:CGRectMake(SCREEN_WIDTH/2-30*kUIScaleBasePlus,18*kUIScaleBasePlus,60*kUIScaleBasePlus,60*kUIScaleBasePlus)
                                      textAligment:NSTextAlignmentCenter
                                          textClor:LCHexColor(0xFF7A17)
                                          textFont:25*kUIScaleBasePlus];
        _headLabel.backgroundColor = LCHexColor(0xFFF6EC);
        _headLabel.layer.masksToBounds = YES;
        _headLabel.layer.cornerRadius = 6;
    }
    return _headLabel;
}

- (UIImageView *)bgImageView
{
    if (!_bgImageView)
    {
        _bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,130*kUIScaleBasePlus)];
        _bgImageView.image = [UIImage imageNamed:@"bg"];
    }
    return _bgImageView;
}

- (UIImageView *)headImageView
{
    if (!_headImageView)
    {
        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-30*kUIScaleBasePlus,25*kUIScaleBasePlus,60*kUIScaleBasePlus,60*kUIScaleBasePlus)];
        _headImageView.backgroundColor = LCHexColor(0xFFF6EC);
        _headImageView.layer.masksToBounds = YES;
        _headImageView.layer.cornerRadius = 6;
    }
    return _headImageView;
}

- (UIButton *)stateButton
{
    if (!_stateButton)
    {
        _stateButton = [Utilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(self.headImageView.frame)+43*kUIScaleBasePlus,14*kUIScaleBasePlus,80*kUIScaleBasePlus,21*kUIScaleBasePlus)
                                         textAligment:UIControlContentHorizontalAlignmentCenter
                                                 text:@"营业中"
                                             textClor:LCHexColor(0xFFFFFF)
                                            buttonBGC:[UIColor clearColor]
                                             textFont:14*kUIScaleBasePlus
                                               target:self
                                               method:@selector(stateClcik)];
        _stateButton.layer.masksToBounds = YES;
        _stateButton.layer.cornerRadius = 6;
        _stateButton.enabled = NO;
    }
    return _stateButton;
}

- (UILabel *)shopLabel
{
    if (!_shopLabel)
    {
        _shopLabel = [Utilties creatLabelWithFrame:CGRectMake(0,CGRectGetMaxY(self.headImageView.frame)+10*kUIScaleBasePlus,SCREEN_WIDTH,21*kUIScaleBasePlus)
                                      textAligment:NSTextAlignmentCenter
                                          textClor:LCHexColor(0xFFFFFF)
                                          textFont:15*kUIScaleBasePlus];
        _shopLabel.text = @"宜兴富安娜家纺滨湖店 章三";
    }
    return _shopLabel;
}

- (UISwitch *)deviceState
{
    if (!_deviceState)
    {
        
        _deviceState = [[UISwitch alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.stateButton.frame)-15*kUIScaleBasePlus,10*kUIScaleBasePlus,34*kUIScaleBasePlus,21*kUIScaleBasePlus)];
        _deviceState.transform = CGAffineTransformMakeScale(0.7,0.7);
        _deviceState.on = YES;
        _deviceState.tintColor = LCHexColor(0xC3C3C3);
        [_deviceState addTarget:self action:@selector(valueChanged:) forControlEvents:(UIControlEventValueChanged)];
    }
    return _deviceState;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self addSubview:self.bgImageView];
        [self addSubview:self.headLabel];
        [self addSubview:self.stateButton];
        [self addSubview:self.shopLabel];
        [self addSubview:self.deviceState];

    }
    return self;
}



#pragma mark - Method
- (void)stateClcik
{
    
}

- (void)valueChanged:(UISwitch *)sender
{
    BOOL isButtonOn = [sender isOn];
    
    NSDictionary *pramaters = @{
                                @"operate" : isButtonOn ? @"1" : @"0"
                                };
    
    WeakSelf(self)
    [infoManager getSwitchOnInfo:pramaters
                          sucess:^(id  _Nonnull acceptData) {
                              NSNumber *num = acceptData[@"data"];
                              LCUserDefaultsSet(num.stringValue, @"switch");
//                              LCUserDefaultsSyn;
                              
                              if (weakSelf.changeStateBlock)
                              {
                                  weakSelf.changeStateBlock();
                              }
                          }
                            fail:^(NSError * _Nullable error) {
                                
                            }];
}

- (void)setModel:(UserInfoModel *)model
{
    _model = model;
    self.shopLabel.text = [NSString stringWithFormat:@"%@ %@",model.shopName,model.userName];
    
    if (model.terminalState.integerValue == 1)
    {
        [self.stateButton setTitle:@"营业中" forState:UIControlStateNormal];
        [self.deviceState setOn:YES];
    }
    else
    {
        [self.stateButton setTitle:@"休息中" forState:UIControlStateNormal];
        [self.deviceState setOn:NO];
    }
    
    self.headLabel.text = [model.shopName substringWithRange:NSMakeRange(0,1)];
}

@end
